package com.jiuli.liping.controller;

import com.jiuli.liping.vo.Classification;
import com.jiuli.liping.vo.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.jiuli.liping.service.ClassificationService;

import java.util.List;

/**
 * Author zyl
 * Date  2020-03-25
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/classification")
public class ClassificationController {
    @Autowired
    private ClassificationService classificationService;

    @RequestMapping(value = {"/list", ""}, method = RequestMethod.GET)
    public Object list() {
        List<Classification> classifications = classificationService.findAllList();
        return classifications;
    }

    @RequestMapping(value = {"/get"}, method = RequestMethod.GET)
    public Object get(@RequestParam String id) {
        Classification classification = classificationService.get(id);
        return classification;
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public String insert(@RequestBody Classification classification) {
        if (classificationService.insert(classification) > 0) {
            return "success";
        } else {
            return "failed";
        }
    }

    @RequestMapping(value = "/insertBatch", method = RequestMethod.POST)
    public String insertBatch(@RequestBody List<Classification> classifications) {
        if (classificationService.insertBatch(classifications) > 0) {
            return "success";
        } else {
            return "failed";
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(@RequestBody Classification classification) {
        if (classificationService.update(classification) > 0) {
            return "success";
        } else {
            return "failed";
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delete(@RequestBody Classification classification) {
        if (classificationService.delete(classification) > 0) {
            return "success";
        } else {
            return "failed";
        }
    }

    /**
     * 获取所有商品
     * @return
     */
    /*@RequestMapping(value = {"/getProductList"}, method = RequestMethod.POST)
    public Object getProductList() {
        List<Classification> classificationList = classificationService.getProductList();
        return classificationList;
    }*/

    /**
     * 根据商品名搜索商品
     * @param product
     * @return
     */
    /*@RequestMapping(value = {"/getProductByName"}, method = RequestMethod.POST)
    public Object getProductByName(@RequestBody Product product) {
        List<Classification> classificationList = classificationService.getProductByName(product);
        return classificationList;
    }*/

    /**
     * 获取目录
     * @return
     */
    @RequestMapping(value = {"/getClassification"}, method = RequestMethod.POST)
    public Object getClassification() {
        List<Classification> classificationList = classificationService.getClassification();
        return classificationList;
    }

    /**
     * 根据目录的pid找到商品
     * @return
     */
    @RequestMapping(value = {"/getProjectByPid"}, method = RequestMethod.POST)
    public List<Product> getProjectByPid(@RequestBody Classification classification) {
        List<Product> projectList = classificationService.getProjectByPid(classification);
        return projectList;
    }
}
